﻿using System.Xml.Serialization;

namespace PersonaLibrary.Properties {
    
    
    // This class allows you to handle specific events on the settings class:
    //  The SettingChanging event is raised before a setting's value is changed.
    //  The PropertyChanged event is raised after a setting's value is changed.
    //  The SettingsLoaded event is raised after the setting values are loaded.
    //  The SettingsSaving event is raised before the setting values are saved.
    internal sealed partial class Settings {
        
        public Settings() {
            // // To add event handlers for saving and changing settings, uncomment the lines below:
            //
            // this.SettingChanging += this.SettingChangingEventHandler;
            //
            this.SettingsSaving += this.SettingsSavingEventHandler;
            System.IO.FileInfo info = new System.IO.FileInfo(System.Environment.CurrentDirectory + @"\custset.xml");
            if (info.Exists)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CustomProperties.SerializableDictionary));
                using (System.IO.FileStream reader = new System.IO.FileStream(System.Environment.CurrentDirectory + @"\custset.xml", System.IO.FileMode.Open))
                {
                    SpecializeProperties = (CustomProperties.SerializableDictionary)serializer.Deserialize(reader);
                }
            }
            else
                SpecializeProperties = new CustomProperties.SerializableDictionary();
        }

        public CustomProperties.SerializableDictionary SpecializeProperties
        {
            private set;
            get;
        }

        private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {
            // Add code to handle the SettingChangingEvent event here.
        }
        
        private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) 
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CustomProperties.SerializableDictionary));
            System.IO.TextWriter writer = new System.IO.StreamWriter(System.Environment.CurrentDirectory + @"\custset.xml");
            serializer.Serialize(writer, SpecializeProperties);
            writer.Close();
        }
    }
}
